import 'dart:convert';

import 'package:banyou_app/app/request/http_request.dart';
import 'package:banyou_app/app/util/common_utils.dart';
import 'package:banyou_app/app/util/crypto_util.dart';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/utils.dart';
import 'package:package_info_plus/package_info_plus.dart';

class RequestMain {
  static Future<Map> getUrl(Map<String, dynamic>? params,
      {String turl = 'https://aqbanyou.olbibo.com.cn/sign/?a=index'}) async {
    String url = await RequestMain.getUrlStr(params, turl);

    final result = await HttpRequest.get(
      url,
    ).then((value) {
      if (value.result) {
        Map map = value.data;
        if (map['code'].toString() == '400') {
          if (map['msg'] != null) {
            EasyLoading.showToast(map['msg']);
            return map;
          }
          return {};
        } else {
          return map;
        }
      } else {
        Map map = value.data ?? {};
        if (map.isNotEmpty && map['data'] != null) {
          EasyLoading.showToast((map['data'] ?? ""));
        }

        return {};
      }
    });
    return result;
  }

  static Future<Map> postUrl(Map<String, dynamic>? params,
      {String turl = 'https://aqbanyou.olbibo.com.cn/sign/?a=index'}) async {
    String url = await RequestMain.getUrlStr(params, turl);
    var encoded = Uri.encodeFull(url);
    final result = await HttpRequest.post(
      encoded,
    ).then((value) {
      if (value.result) {
        Map map = value.data;
        if (map['code'].toString() == '400') {
          if (map['msg'] != null) {
            EasyLoading.showToast(map['msg'] ?? "");
            return map;
          }
          return {};
        } else {
          return map;
        }
      } else {
        Map map = value.data;
        EasyLoading.showToast(map['data'].toString());
        return {};
      }
    });
    return result;
  }

  static Future<String> getUrlStr(
      Map<String, dynamic>? params, String turl) async {
    String dfxusf_tokfn = box.read("dfxusf_tokfn") ?? "";
    String udea = box.read("udea") ?? "";
    String tdud = box.read("tdud") ?? "";
    String lng = box.read("lng") ?? "0.0";
    String lat = box.read("lat") ?? "0.0";
    String temp = DateTime.now().hour > 9
        ? "aqbanyou" + DateTime.now().hour.toString()
        : "aqbanyou" + "0" + DateTime.now().hour.toString();
    String signature = CryptoUtil.generateMD5(temp);
    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
    IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    dynamic key = await box.read('key') ?? "";
    Map tempMap = {
      'versnai': packageInfo.version,
      // 'slufnt':"iphone",
      'ybneit': Uri.encodeComponent(
          (iosInfo.name ?? "").replaceAll(new RegExp(r"\s+\b|\b\s"), "")),
      'devnye': iosInfo.utsname.machine,
      'ndfo': udea,
      'devnye_takei': dfxusf_tokfn,
      'tdnd': tdud, //TakingData 唯一ID(tdid)
      'big': lng,
      'bot': lat,
      'key': key,
      'signature': signature,
      "appname": 'aqbanyou',
      ...params ?? {},
    };

    //
    tempMap.forEach((key, value) {
      if (key.contains('result')) {
        value = json.encode(value);
        turl += ('&${key.toString()}=${value.toString()}');
      } else {
        turl += ('&$key=$value');
      }
    });

    return turl;
  }
}
